package com.smartleanx.module.hrm.service.employee;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smartleanx.module.hrm.controller.admin.employee.certificate.EmployeeCertificateSaveReqVO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeWorkExperienceDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeCertificateDO;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.object.BeanUtils;

import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeCertificateMapper;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.*;

/**
 * 员工证书 Service 实现类
 *
 * @author 精益大师
 */
@Service
@Validated
public class EmployeeCertificateServiceImpl implements EmployeeCertificateService {

    @Resource
    private EmployeeCertificateMapper employeeCertificateMapper;
    @Resource
    private EmployeeService employeeService;

    @Override
    public Long createEmployeeCertificate(EmployeeCertificateSaveReqVO createReqVO) {
        // 插入
        EmployeeCertificateDO employeeCertificate = BeanUtils.toBean(createReqVO, EmployeeCertificateDO.class);

        // 校验时间有效性
        validateCertificateTime(createReqVO);

        List<EmployeeCertificateDO> certificateList = getEmployeeCertificateList(createReqVO.getEmployeeId());
        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(createReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        employeeCertificate.setSort(CollUtil.isEmpty(certificateList) ? 0 : certificateList.get(0).getSort() + 1);
        employeeCertificateMapper.insert(employeeCertificate);
        // 返回
        return employeeCertificate.getId();
    }

    @Override
    public void updateEmployeeCertificate(EmployeeCertificateSaveReqVO updateReqVO) {
        // 校验存在
        validateEmployeeCertificateExists(updateReqVO.getId());
        // 校验时间有效性
        validateCertificateTime(updateReqVO);
        // 校验员工是否存在
        EmployeeDO employeeDO = employeeService.getEmployeeById(updateReqVO.getEmployeeId());
        if (employeeDO == null) {
            throw exception(EMPLOYEE_NOT_EXISTS);
        }
        // 更新
        EmployeeCertificateDO updateObj = BeanUtils.toBean(updateReqVO, EmployeeCertificateDO.class);
        employeeCertificateMapper.updateById(updateObj);
    }

    public void validateCertificateTime(EmployeeCertificateSaveReqVO vo){
        if (Objects.nonNull(vo.getStartTime()) && Objects.nonNull(vo.getEndTime())) {
            if (vo.getStartTime().compareTo(vo.getEndTime()) > 0) {
                throw exception(CERTIFICATE_START_TIME_UNLATER_END_TIME);
            }
        }
    }

    @Override
    public void deleteEmployeeCertificate(Long id) {
        // 校验存在
        validateEmployeeCertificateExists(id);
        // 删除
        employeeCertificateMapper.deleteById(id);
    }

    private void validateEmployeeCertificateExists(Long id) {
        if (employeeCertificateMapper.selectById(id) == null) {
            throw exception(EMPLOYEE_CERTIFICATE_NOT_EXISTS);
        }
    }

    @Override
    public EmployeeCertificateDO getEmployeeCertificate(Long id) {
        return employeeCertificateMapper.selectById(id);
    }

    @Override
    public List<EmployeeCertificateDO> getEmployeeCertificateList(Long employeeId) {
        return employeeCertificateMapper.getEmployeeCertificateList(employeeId);
    }

    @Override
    public void deleteByEmployeeId(Long employeeId) {
        employeeCertificateMapper.delete(new LambdaQueryWrapper<EmployeeCertificateDO>(){{
            eq(EmployeeCertificateDO::getEmployeeId,employeeId);
        }});
    }

}
